﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VaccineEbook.Models.DBEntity
{
	public partial class Vaccine
	{
        internal static IEnumerable<Vaccine> QuickSearch(string _strSeach, ref string _messageError)
        {
            VaccineEBookEntities context = null;
            try
            {
                context = new VaccineEBookEntities(Constant.DEFAUT_TIMEOUT_SECOND);
                var query = from v in context.Vaccines
                            where string.IsNullOrEmpty(_strSeach) ||  
                            v.VaccineName.Contains(_strSeach)
                            orderby v.VaccineName
                            select v;
                return query;
            }
            catch (Exception ex)
            {
                _messageError = ex.Message;
                return null;
            }
            finally
            {
                context = null;
                GC.Collect();
            }
        }

        internal static Vaccine GetBy_VaccineID(int _vaccineId, ref string _messageError)
        {
            VaccineEBookEntities context = null;
            try
            {
                context = new VaccineEBookEntities();
                var query = from v in context.Vaccines
                            where v.VaccineID == _vaccineId
                            select v;
                return query.First();
            }
            catch (Exception ex)
            {
                _messageError = ex.Message;
                return null;
            }
            finally
            {
                context = null;
                GC.Collect();
            }

        }

        internal static Vaccine AddNew(Vaccine _vaccine, int _createBy, ref string _messageError)
        {
            VaccineEBookEntities context = null;
            try
            {
                context = new VaccineEBookEntities();
                _vaccine.CreateBy = _createBy;
                _vaccine.Status = 1;
                _vaccine.CreateDate = DateTime.Now;
                context.Vaccines.Add(_vaccine);
                context.SaveChanges();
                return _vaccine;
            }
            catch (Exception ex)
            {
                _messageError = ex.Message;
                return null;
            }
            finally
            {
                context = null;
                GC.Collect();
            }
        }

        internal static Vaccine Update(int _vaccineID, string _vaccineName, ref string _messageError)
        {
            VaccineEBookEntities context = null;
            try
            {
                context = new VaccineEBookEntities();
                var query = from v in context.Vaccines
                            where v.VaccineID == _vaccineID
                            select v;
                Vaccine vaccine = query.First();
                vaccine.VaccineName = _vaccineName;
                context.SaveChanges();
                return vaccine;
            }
            catch (Exception ex)
            {
                _messageError = ex.Message;
                return null;
            }
            finally
            {
                context = null;
                GC.Collect();
            }
        }

        internal static Vaccine Delete(int _vaccineId, ref string _messageError)
        {
            VaccineEBookEntities context = null;
            try
            {
                context = new VaccineEBookEntities();
                var vaccine = new Vaccine { VaccineID = _vaccineId };
                context.Vaccines.Attach(vaccine);
                context.Vaccines.Remove(vaccine);
                context.SaveChanges();
                return vaccine;
            }
            catch (Exception ex)
            {
                _messageError = ex.Message;
                return null;
            }
            finally
            {
                context = null;
                GC.Collect();
            }
        }
    }
}